package main

//https://leetcode-cn.com/problems/house-robber-iii/
/**
* Definition for a binary tree node.
 */
type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func rob(root *TreeNode) int {
	if root == nil {
		return 0
	}
	var leftSum, rightSum int
	if root.Left != nil {
		leftSum = rob(root.Left.Left) + rob(root.Left.Right)
	}
	if root.Right != nil {
		rightSum = rob(root.Right.Left) + rob(root.Right.Right)
	}
	return Max(root.Val+leftSum+rightSum, rob(root.Left)+rob(root.Right))
}

//int val = 0;
//if(root.left != null) val += rob(root.left.left) + rob(root.left.right);
//if(root.right != null) val += rob(root.right.left) + rob(root.right.right);
//return Math.max(rob(root.left) + rob(root.right),val + root.val);

func Max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	t := &TreeNode{
		Val: 3,
		Left: &TreeNode{
			Val: 2,
			//Left:  &TreeNode{Val: 2},
			Right: &TreeNode{Val: 3},
		},
		Right: &TreeNode{
			Val: 3,
			//Left:  &TreeNode{Val: 1},
			Right: &TreeNode{Val: 1},
		},
	}
	println(rob(t))
}
